import os

import requests
from dotenv import dotenv_values

config = dotenv_values('.flaskenv')
# MySql配置信息
appID = config.get('appID') or 'wx2be380fdde5b4d4f'
appSecret = config.get('appSecret') or 'aab61b48e5b9b7d45485ac93feaa9107'


def get_openid(code):
    req_params = {
        'appid': appID,
        'secret': appSecret,
        'js_code': code,
        'grant_type': 'authorization_code'
    }
    wx_login_api = 'https://api.weixin.qq.com/sns/jscode2session'
    response_data = requests.get(wx_login_api, params=req_params)  # 向API发起GET请求
    resData = response_data.json()
    # print(resData)
    openid = resData['openid']
    return openid

 # 获取接口调用凭证
 # 021fVEkl2MHAV84YoTnl2Nefbs2fVEkB
def token(code):
    print(code)
    wx_getAccessToken = 'https://api.weixin.qq.com/cgi-bin/token'
    AccessToken_params = {
        'grant_type': 'client_credential',
        'appid': appID,
        'secret': appSecret,
        'js_code': code
    }
    response_token = requests.get(wx_getAccessToken, params=AccessToken_params)
    response_token_data = response_token.json()
    # print('response_token_data：',response_token_data)
    response_access_token = response_token_data.get('access_token')
    print('response_token_data:',response_access_token)
    return response_access_token


if __name__ == '__main__':
    # openid=get_openid("001x4I0w3ZQj0Y2Sc60w3O5IrE4x4I0t")
    # print(openid)
    # base_path = os.getcwd()
    # print(base_path)
    # token()pa
    pass